package redisdemo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:application-redis.xml")
public class StringRedisTest {

    @Resource
    RedisTemplate redisTemplate;

    //设置String缓存
    @Test
    public void opsForValueTest(){
        //设置无过期时间的缓存
        redisTemplate.opsForValue().set("xx","小不点");
        Object xx = redisTemplate.opsForValue().get("xx");

        //设置有过期时间的缓存,十秒 set void set(K key, V value, long timeout, TimeUnit unit);
        redisTemplate.opsForValue().set("jj","金金",10,TimeUnit.SECONDS);
        Object jj = redisTemplate.opsForValue().get("jj");

        //偏移并覆盖 set void set(K key, V value, long offset);
        redisTemplate.opsForValue().set("xz","xz湘大猪猪");
        Object xz = redisTemplate.opsForValue().get("xz");
        System.out.println(xz);
        redisTemplate.opsForValue().set("xz","小猪猪",2);
        Object xz1 = redisTemplate.opsForValue().get("xz");
        System.out.println(xz1);
        redisTemplate.opsForValue().set("xz","湘",5);
        Object xz2 = redisTemplate.opsForValue().get("xz");
        System.out.println(xz2);
    }

    // boolean setIfAbsent(K key)
    // 是否缺少key
    @Test
    public void setIfAbsentTest(){
        Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent("xz", "湘");
        System.out.println(aBoolean);//false
    }

}
